#include <stdio.h>
#include <stdlib.h>

typedef struct Student
{
    int id;
    char name[50];
    float score;
}Student;

Student* createStudents(int n)
{
    struct Student *students;
    students = (struct Student*)malloc(n*sizeof(struct Student));
    
    if(students == NULL)
    {
        printf("内存分配失败！\n");
        return NULL;
    }

    for(int i = 0;i < n; i++)
    {
        printf("请输入第%d个学生的id:\n",i+1);
        scanf("%d",&students[i].id);

        printf("请输入第%d个学生的姓名:\n",i+1);
        scanf("%s",students[i].name);

        printf("请输入第%d个学生的score:\n",i+1);
        scanf("%f",&students[i].score);
    }
    return students;
}

void printStudents(Student* students, int n)
{
    for(int i = 0; i < n; i++)
    {
        printf("id: %d ,",students[i].id);
        printf("name: %s ,",students[i].name);
        printf("score: %.1f\n",students[i].score);
    }
}

void freeStudents(Student* students)
{
    free(students);
}

void sortStudentsByScore(Student* students, int n)
{
    for(int i = 0; i < n-1;i++)
    {
        for(int j = 0; j < n-1-i; j++)
        {
            if(students[j].score < students[j+1].score)
            {
                Student temp = students[j];
                students[j] = students[j+1];
                students[j+1] = temp;
            }
        }
    }
}

int main(int argc, char const *argv[])
{
    int n;
    printf("请输入学生的数量：\n");
    scanf("%d",&n);

    Student *students = createStudents(n);

    printf("排序前：\n");
    printStudents(students,n);

    sortStudentsByScore(students,n);

    printf("排序后：\n");
    printStudents(students,n);

    freeStudents(students);
    
    return 0;

}
